<template>
  <div id="sidebar">
    <el-row class="tac" style="height: 100%">
      <el-col :span="12" style="height: 100% ;width: 100%" >
        <el-menu
            default-active="2"
            class="el-menu-vertical-demo"
            @open="handleOpen"
            @close="handleClose"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#ffd04b"
            style="height: 100%">
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span >住宿变更</span>
            </template>
            <el-menu-item @click="handleDormAdjust">住宿调整申请</el-menu-item>
            <el-menu-item @click="handleInDorm">临时入住申请</el-menu-item>
            <el-menu-item @click="handleUnCheck">退宿申请</el-menu-item>
            <el-menu-item @click="handleCheckPoint">查看寝室评分情况</el-menu-item>
            <el-menu-item @click="handleCheckInd">查看违纪情况</el-menu-item>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span >维修事宜</span>
            </template>
            <el-menu-item @click="handleRepair">维修申请</el-menu-item>
            <el-menu-item @click="handleWatchRepair">维修记录查看</el-menu-item>
          </el-submenu>
        </el-menu>
      </el-col>
    </el-row>
    <DormitoryAdjustDialog ref="DormitoryAdjustDialog"></DormitoryAdjustDialog>
    <RoomExchangeDialog ref="RoomExchangeDialog"></RoomExchangeDialog>
    <repair-dialog ref="repairDialog"></repair-dialog>
  </div>

</template>
<script>
import DormitoryAdjustDialog from './adjustDormitoryDialog/DormitoryAdjustDialog.vue'
import RoomExchangeDialog from './adjustDormitoryDialog/RoomExchangeDialog.vue'
import repairDialog from './repairDialog.vue'
import {_getRating, _handleInDorm, _unCheckRoom} from '../../../api/student'
export default {
  components: {
    DormitoryAdjustDialog,
    RoomExchangeDialog,
    repairDialog
  },
  methods: {
    handleOpen (key, keyPath) {
      console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      console.log(key, keyPath)
    },
    handleDormAdjust () {
      if (JSON.parse(localStorage.getItem('user')).room === 0 || JSON.parse(localStorage.getItem('user')).room === null) {
        this.$message({
          message: '您还未分配房间，无法调整寝室',
          type: 'error'
        })
        return
      }
      this.$refs.DormitoryAdjustDialog.show()
    },
    handleRepair () {
      this.$refs.repairDialog.show()
    },
    handleInDorm () {
      if (JSON.parse(localStorage.getItem('user')).room !== null && JSON.parse(localStorage.getItem('user')).room !== 0) {
        this.$message({
          message: '您已经有房间，无法请求入住',
          type: 'error'
        })
        return
      }
      this.$confirm('确定提交入住申请？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _handleInDorm(JSON.parse(localStorage.getItem('user')).id).then(res => {
          if (res.status) {
            this.$message({
              message: '入住申请提交成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '入住申请提交失败',
              type: 'error'
            })
          }
        })
      }
      ).catch()
    },
    // handleRoomExchange () {
    //   this.$refs.RoomExchangeDialog.show()
    // },
    handleUnCheck () {
      if (JSON.parse(localStorage.getItem('user')).room === 0 || JSON.parse(localStorage.getItem('user')).room === null) {
        this.$message({
          message: '您还未分配房间，无法退宿',
          type: 'error'
        })
        return
      }
      this.$confirm('确定提交退宿申请？', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        _unCheckRoom(JSON.parse(localStorage.getItem('user'))).then(res => {
          if (res.status) {
            this.$message({
              message: '退宿申请提交成功',
              type: 'success'
            })
          } else {
            this.$message({
              message: '退宿申请提交失败',
              type: 'error'
            })
          }
        })
      }
      ).catch()
    },
    transform_data (grade) {
      if (grade === 0) { return '无星级' } else if (grade === 1) { return '一星级' } else if (grade === 2) { return '二星级' } else if (grade === 3) { return '三星级' } else if (grade === 4) { return '四星级' } else if (grade === 5) { return '五星级' }
    },
    handleCheckPoint () {
      _getRating(JSON.parse(localStorage.getItem('user')).room).then(res => {
        if (res.status) {
          this.$message({
            message: '您最近的寝室评分为' + this.transform_data(res.data),
            type: 'success'
          })
        } else {
          this.$message({
            message: '未知错误',
            type: 'error'
          })
        }
      })
    },
    handleCheckInd () {
      console.log('hahahah')
      this.$router.push({name: 'indisciplinetable'})
    },
    handleWatchRepair () {
      this.$router.push({name: 'repairtable'})
    }
  }
}
</script>

<style scoped>
#sidebar{
  height: 100%;
}
</style>
